﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

namespace JetRetail
{
    public partial class ReturnMoneyForm : Form
    {
        int iRegistratorType;
        DrvFRLib.IDrvFR47 ShtrihM;
        private MercFPrtX.MercuryFPrtX Gepard;
        public MainForm mainForm = null;
        //private ISingleResult<ПолучитьПозицииЧекаResult> Result;
        private List<ПолучитьПозицииЧекаResult> lstResult;
        private List<ПолучитьПозицииЧекаResult> lstResultItog;
        private int itypePayment = -1;

        public ReturnMoneyForm(int iRegistratorType, DrvFRLib.IDrvFR47 ShtrihM, MercFPrtX.MercuryFPrtX Gepard)
        {
            this.iRegistratorType=iRegistratorType;
            this.ShtrihM = ShtrihM;
            this.Gepard = Gepard;

            //this.ParentForm= mainForm;
            
            InitializeComponent();
            comboBoxoperation.SelectedIndex = 0;
            FRKSelect(iRegistratorType);
        }

        private void FRKSelect(int iRegistratorType)
        {
            switch (iRegistratorType)
            {
                case 1:
                case 2:
                    {
                        trReturn.Enabled = false;
                        txtNCheck.Text = "0";

                        txtNCheck.Enabled = false;
                        btnFind.Enabled = false;
                        trReturn.Enabled = false;
                    }
                    break;
                case 3:
                    {
                        comboBoxoperation.Enabled = false;
                        TextToPrint.Enabled = false;
                        Sum.Enabled = false;
                    }
                    break;
            }
        }

        void ValidateInfo()
        {
            OK.Enabled = ((TextToPrint.Text != "") && (Sum.Text != ""));
        }

        private void TextToPrint_TextChanged(object sender, EventArgs e)
        {
            ValidateInfo();
        }

        private void Sum_TextChanged(object sender, EventArgs e)
        {
            ValidateInfo();
        }

        bool ReturnMoney(string sTextToPrint, decimal deSum, int TypeCach)
        {
            try
            {
                switch (iRegistratorType)
                {
                    #region 

                        /*case 1:
                        {
                            Atol.Name = sTextToPrint;
                            Atol.Quantity = 1;
                            Atol.Price = (double)deSum;
                            Atol.EnableCheckSumm = true;
                            Atol.DiscountValue = 0;
                            if (Atol.Return() != 0)
                            {
                                MessageBox.Show(Atol.ResultDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                            Atol.TypeClose = 0;
                            if (Atol.CloseCheck() != 0)
                            {
                                MessageBox.Show(Atol.ResultDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                        } 
                        break;*/

                        #endregion
                    case 1:
                    case 2:
                        {
                            ShtrihM.StringForPrinting = sTextToPrint;
                            ShtrihM.Quantity = 1;
                            ShtrihM.Price = deSum;
                            if (ShtrihM.ReturnSaleEx() != 0)
                            {
                                MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                            if (TypeCach == 0)
                            {
                                ShtrihM.Summ1 = deSum;
                                ShtrihM.Summ2 = 0;
                                ShtrihM.Summ3 = 0;
                                ShtrihM.Summ4 = 0;
                            }
                            if (TypeCach == 1)
                            {
                                ShtrihM.Summ1 = 0;
                                ShtrihM.Summ2 = deSum;
                                ShtrihM.Summ3 = 0;
                                ShtrihM.Summ4 = 0;
                            }
                            if (TypeCach == 2)
                            {
                                ShtrihM.Summ1 = 0;
                                ShtrihM.Summ2 = 0;
                                ShtrihM.Summ3 = deSum;
                                ShtrihM.Summ4 = 0;
                            }

                            ShtrihM.DiscountOnCheck = 0;
                            ShtrihM.StringForPrinting = "";
                            if (ShtrihM.CloseCheck() != 0)
                            {
                                MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                        }
                        break;
                    case 3:
                        {
                            //****************
                            lstResultItog = new List<ПолучитьПозицииЧекаResult>();
                            
                            int i = 0;
                            foreach (TreeListNode objItem in trReturn.Nodes)
                            {
                                if (Convert.ToBoolean(objItem.GetValue(colSel))==true)// если элемент выделен   
                                {
                                    lstResultItog.Add(lstResult[i]);
                                }
                                i++;
                            }

                            if (lstResultItog.Count != 0)
                            {
                                if (itypePayment == 1) // оплата наличными
                                {
                                    MainForm.Instance.RegisterCheckPositions(0, 0, false, false, lstResultItog,0,"");
                                }
                                else
                                {
                                    MainForm.Instance.RegisterCheckPositions(0, 0, false, true, lstResultItog,0,"");
                                }     
                            }                           
                        }
                        break;
                    case 4 :
                        {
                            if (TypeCach == 0)
                            {
                                MainForm.Instance.RegisterCheckPositions(deSum, 0, false, false, lstResultItog, 0, sTextToPrint);
                            }
                            if (TypeCach == 1)
                            {
                                MainForm.Instance.RegisterCheckPositions(0, deSum, false, true, lstResultItog, 0, sTextToPrint);
                            }

                            
                        }
                        break;
                }

                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (iRegistratorType == 2)
                {
                    if (ReturnMoney(TextToPrint.Text, decimal.Parse(Sum.Text), comboBoxoperation.SelectedIndex) == true) Close();
                }
                if (iRegistratorType == 3)
                {
                    if (ValidateOk())
                    {
                        if (ReturnMoney("", 0, 0) == true) Close();
                    }
                }
                if (iRegistratorType==4)
                {
                    if (ReturnMoney(TextToPrint.Text, decimal.Parse(Sum.Text), comboBoxoperation.SelectedIndex) == true) Close();
                }
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (txtNCheck.Text.Count() == 0)
            {
                MessageBox.Show("Номер чека не указан", "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            int nCheck = Convert.ToInt32(txtNCheck.Text);

            int KeyCheck = MainForm.Instance.DB.GetCheckKeyByNum(nCheck);
            itypePayment = MainForm.Instance.DB.GetTypePaymentByCheckNum(nCheck);

            //ISingleResult<ПолучитьПозицииЧекаResult> Result = MainForm.Instance.DB.GetPositionCheck(KeyCheck);
            lstResult = MainForm.Instance.DB.GetPositionCheck(KeyCheck).ToList();

            if (lstResult.Count() == 0)
            {
                MessageBox.Show("Чек не найден", "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Information);
                OK.Enabled = false;
            }
            else
            {
                trReturn.DataSource = lstResult;

                //lstResult = MainForm.Instance.DB.GetPositionCheck(KeyCheck).ToList(); //Result.ToList();

                foreach (TreeListNode objItem in trReturn.Nodes)
                {
                    objItem.SetValue(colSel, CheckState.Checked);
                }
                
                OK.Enabled = true;
            }
            
           
        }

        private bool ValidateOk()
        {
            int i = 0;
            foreach (TreeListNode objItem in trReturn.Nodes)
            {
                if (Convert.ToBoolean(objItem.GetValue(colSel)) == true)
                {
                    i++;
                }
            }
            if (i == 0)
            {
                MessageBox.Show("Нет выбранных позиций, на которые нужно оформить возврат","Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else
            {
                return true;
            }
        }
      
    }
}